package com.cai.service.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.cai.dao.CheckGroupDao;
import com.cai.dao.CheckItemDao;
import com.cai.dao.SetmealDao;
import com.cai.entity.PageResult;
import com.cai.entity.QueryPageBean;
import com.cai.pojo.CheckGroup;
import com.cai.pojo.CheckItem;
import com.cai.pojo.Setmeal;
import com.cai.service.SetmealService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealDao setmealDao;

    @Autowired
    private CheckGroupDao checkGroupDao;
    @Autowired
    private CheckItemDao checkItemDao;

    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        //1.添加setmeal表
        setmealDao.add(setmeal);
        //2.关联中间表
        if (checkgroupIds != null && checkgroupIds.length > 0) {
            //绑定套餐和检查组的多对多关系
            setSetmealAndCheckGroup(setmeal.getId(), checkgroupIds);
        }
    }
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        //使用分页助手
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<Setmeal> setmeals = setmealDao.selectByCondition(queryPageBean.getQueryString());
        return new PageResult(setmeals.getTotal(), setmeals.getResult());
    }

    //查询所有套餐信息
    public List<Setmeal> findAll() {
        List<Setmeal> list = setmealDao.findAll();
        return list;
    }

    //根据id查询套餐信息和套餐包含的检查组信息及检查组包含的检查项信息
    public Setmeal findById(Integer id) {
        Setmeal setmeal = setmealDao.findById(id);
        List<Integer> checkgroupBySetmealId = setmealDao.findCheckgroupBySetmealId(id);
        List<CheckGroup>checkGroupList=new ArrayList<>();

        for (Integer checkgroup_id : checkgroupBySetmealId) {
            //查询检查组信息
            CheckGroup checkGroup = checkGroupDao.findById(checkgroup_id);
            checkGroupList.add(checkGroup);
            List<CheckItem>checkItemList=new ArrayList<>();
            //查询检查项信息
            List<Integer> checkItemIdsByCheckGroupId = checkGroupDao.findCheckItemIdsByCheckGroupId(checkgroup_id);
            for (Integer chekcitem_id : checkItemIdsByCheckGroupId) {
                CheckItem checkItem = checkItemDao.findById(chekcitem_id);
                checkItemList.add(checkItem);
            }
            checkGroup.setCheckItems(checkItemList);
        }
        setmeal.setCheckGroups(checkGroupList);
        return setmeal;
    }

    @Override
    public List<Map<String, Object>> findCountSetmeal() {
        return setmealDao.findCountSetmeal();
    }

    /**
     * 页面端查询所有套餐信息
     * @return
     */
    @Override
    public List<Setmeal> findAllSetmeal() {
        List<Setmeal> list = setmealDao.findAll();
        for (Setmeal setmeal : list) {
            String sex = setmeal.getSex();
            if (sex.equals("0")){
                sex = "不限";
            }
            if (sex.equals("1")){
                sex = "男性";
            }
            if (sex.equals("2")){
                sex = "女性";
            }
            setmeal.setSex(sex);
        }
        return list;
    }


    //套餐和检查组的多对多关系
    private void setSetmealAndCheckGroup(Integer id, Integer[]
            checkgroupIds) {
        for (Integer checkgroupId : checkgroupIds) {
            Map<String, Integer> map = new HashMap<>();
            map.put("setmeal_id", id);
            map.put("checkgroup_id", checkgroupId);
            setmealDao.setSetmealAndCheckGroup(map);
        }
    }
}
